<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>基础语法6 v-for条件渲染</title>
    <script src="http://unpkg.com/vue@3"></script>
</head>
<body>
    <div id="app">
        <h1>列表渲染主要通过v-for指令来实现</h1>
        <h1>知识点1：迭代普通数组</h1>
        <ul>
            <li v-for="item in times">桃花</li>
        </ul>
        <ul>
            <li v-for="item in flowers">{{item}}</li>
        </ul>
        <h1>知识点2：迭代对象数组</h1>
        <ul>
            <li v-for="(item,index) in users">{{index+1}}--{{item.id}}--{{item.name}} </li>
        </ul>
        <h1>知识点3：迭代数字</h1>
        <p v-for="item in 100">{{item}}</p>
        <h1>知识点4：关于key属性</h1>
        <h3>为了提升循环的性能，我们一般会给每个循环加上唯一的一个key值。
            在实际中，后端会向前端返回数据时，携带唯一的数据相关的数据条目标识
        </h3>
<p>例子</p>

   
     ID:<input type="text" v-model="id"><br>
     NAME:<input type="text" v-model="name"><br>
     <button @click="add()">尾部添加</button>
     <button @click="del()">尾部删除</button>
     <button @click="add1()">头部添加</button>
     <button @click="del1()">头部删除</button>
    <table border="1">
        <tr v-for="(item,index) in users":key="item.id">
            <td>
                {{item.id}}
            </td>
            <td>
                {{item.name}}
            </td>
        </tr>
    </table>
    
    <h1>知识点5：v-for的嵌套</h1>
    <p>主要针对对对象数组的迭代</p>
    <ul>
        <li v-for="(item,index) in users ":key='index'>用户{{index+1}}
    <p v-for="(list,i)in item">{{list}}</p>
        </li>
    </ul>
 </div>
</body>
</html>
<script>
    const connApp =Vue.createApp({
    data(){
        return{
            times:10,
            flowers:['西蓝花','鸡米花','菜花','爆米花','有钱花'],
            users:[{id:'001',name:'七七八'},
                {id:'002',name:'钵钵鸡'},
                {id:'003',name:'大狗' },

            ],
            id:"",
            name:"",
        }
    },
    methods: {
        add(){
            this.users.push({id:this.id,name:this.name})
        },
        del(){
            this.users.pop()
        },
        add1(){
            this.users.unshift({id:this.id,name:this.name})
        },
        del1(){
            this.users.shift()
        }
    },
    }).mount("#app")
</script>